package com.wanmait.wanmarecruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.wanmarecruit.annotation.ManageLogAnnotation;
import com.wanmait.wanmarecruit.service.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wanmarecruit.mapper.UserMapper;
import com.wanmait.wanmarecruit.entity.User;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;
    @Value("${hash.algorithm-name}")
    private String hashName;
    @Value("${hash.iterations}")
    private int hashIterations;

    //修改密码
    @Override
    public void changePassword(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone,user.getPhone());
        User user1 = userMapper.selectOne(queryWrapper);
        SimpleHash simpleHash = new SimpleHash(hashName,user.getPass(),user1.getSalt(),hashIterations);
        user.setPass(simpleHash.toString());
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getPhone,user.getPhone());
        this.update(user,updateWrapper);
    }

    //验证手机号是否以注册
    @Override
    public Boolean signUpVerify(String phone) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(phone!=null,User::getPhone,phone);
        User user = userMapper.selectOne(queryWrapper);
        return user!=null;
    }

    //检测是否登录
    @Override
    public User LoginOrNot(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(user.getPhone()!=null,User::getPhone,user.getPhone());
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1==null)
        {
            return null;
        }
        SimpleHash simpleHash = new SimpleHash(hashName,user.getPass(),user1.getSalt(),hashIterations);
        if (simpleHash.toString().equals(user1.getPass()))
        {
            return user1;
        }
        return null;
    }

    //验证登录手机号，是否已注册
    @Override
    public Boolean verifySignUp(String phone) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(phone!=null,User::getPhone,phone);
        return this.getOne(queryWrapper)!=null;
    }

    @Override
    public User getUserByPhone(String phone) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone,phone);
        return this.getOne(queryWrapper);
    }

    @Override
    public User selectOneUser(Integer userId) {
        return userMapper.selectOneUser(userId);
    }

    @Override
    public Boolean isUserResume(Integer userId) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,userId);
        queryWrapper.eq(User::getResume,true);
        return this.getOne(queryWrapper)!=null;
    }

    @Override
    public User getUserMessageByUserId(Integer userId) {
        return userMapper.selectUserMessageByUserId(userId);
    }

    @Override
    public Boolean checkPass(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,user.getId());
        queryWrapper.eq(User::getPass,user.getPass());
        return this.getOne(queryWrapper)!=null;
    }

    @Override
    public void updateUserNotEnable(User user) {
        userMapper.updateUserNotEnable(user);
    }

    @Override
    public void updateEnableTrueByUserId(Integer userId) {
        userMapper.updateEnableTrueByUserId(userId);
    }

    //封禁用户（张佳炜）
    @Override
    @ManageLogAnnotation(module = "用户管理",info = "已将该用户封禁")
    public void updateUserEnableFalseByUserId(Integer userId) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("enable",0).in("id",userId);
        this.update(updateWrapper);
    }

    @Override
    public Long getUserCount() {
        return this.count();
    }

    @Override
    public User signInOrNot(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(user.getPhone()!=null,User::getPhone,user.getPhone());
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1==null)
        {
            return null;
        }

        return user1;
    }

    @Override
    public PageInfo<User> getAllUserMessage(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getName,keyword);
        return new PageInfo<>(this.list(queryWrapper));
    }



}
